//
//  MyKeChengViewController.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/4/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "MyKeChengViewController.h"
//#import "MyKeCHengTableViewCell.h"
#import "AdetailViewController.h"
#import "ShouCangTableViewCell.h"

@interface MyKeChengViewController ()<UITableViewDelegate,UITableViewDataSource>

//@property (nonatomic, strong)UITableView *tableView;
@property(nonatomic,strong)AxcEmptyDataTableView *tableView;

@property (nonatomic, strong)NSMutableArray *dataArray;

@end

@implementation MyKeChengViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的课程";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
   [self addTableView];
    
    [self loadData];
    
    [self.tableView registerClass:[ShouCangTableViewCell class] forCellReuseIdentifier:@"MyKeCHengTableViewCell"];

}

-(void)loadData{
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Course&a=buyCourseList",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);

        _dataArray = [NSMutableArray array];
        
        NSDictionary *dic = responseObject;
        
        if ([dic[@"status"] intValue] == 200) {
            for (NSDictionary *dicc in dic[@"data"][@"course_list"]) {
                [_dataArray addObject:dicc];
            }
           
            if (_dataArray.count < 4) {
                _tableView.mj_footer.hidden = YES;
            }else{
                
                _tableView.mj_footer.hidden = NO;
            }
            
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:1.5f];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
}

-(void)addTableView {
        _tableView = [[AxcEmptyDataTableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight - NAVIGATION_BAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate        = self;
        _tableView.dataSource      = self;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
        _tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
    
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];

    self.placeholderView.frame = _tableView.bounds;
    
    // 设置空集时候展示的View
    _tableView.axcUI_placeHolderView = self.placeholderView;
    
    //  默认YES 自带渐入渐出动画效果，下边这个参数可以关闭
    _tableView.axcUI_placeHolderViewAnimations = NO;
    
    // 占位View的“重新加载”按钮的Block回调:
    __weak typeof(self) WeakSelf = self;
    
    self.placeholderView.clickLoadBtnBlock = ^(id type) {
        //        WeakSelf.dataArray = nil;
        
        [WeakSelf loadData];
        
    };
    
    [self.view addSubview:_tableView];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


 -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
 
 return 0;
     
 }
 
 #pragma mark -- UITableViewDataSource,UITableViewDelegate
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return _dataArray.count;
 }
 
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     static NSString *identifity = @"MyKeCHengTableViewCell";
     //    MyIndentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifity];
     ShouCangTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     if (cell == nil) {
         cell = [[ShouCangTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifity];
     }
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
     
     cell.nameLable.text = [_dataArray[indexPath.row] valueForKey:@"name"];
//     cell.keshiLable.text = [NSString stringWithFormat:@"课时：%@", [_dataArray[indexPath.row] valueForKey:@"name"]];
     [cell.imageVC sd_setImageWithURL:[NSURL URLWithString:[_dataArray[indexPath.row] valueForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"0009"]];
     
     cell.guanZhuBtn.hidden = YES;
     
//     CGFloat height = [self getLabelHeightWithText:cell.nameLable.text width:ScreenWidth - 200 * KscreenWidth font:15 * KscreenHeight];
//     if (height > 20.0) {
//         cell.nameLable.frame  = CGRectMake(160 * KscreenWidth, 20 * KscreenHeight, ScreenWidth - 170 * KscreenWidth, 40 * KscreenHeight);
//         cell.keshiLable.frame = CGRectMake(160 * KscreenWidth, 60 * KscreenHeight, ScreenWidth - 200 * KscreenWidth, 15 * KscreenHeight);
//     }else{
//         cell.nameLable.frame  = CGRectMake(160 * KscreenWidth, 20 * KscreenHeight, ScreenWidth - 170 * KscreenWidth, 20 * KscreenHeight);
//         cell.keshiLable.frame = CGRectMake(160 * KscreenWidth, 50 * KscreenHeight, ScreenWidth - 200 * KscreenWidth, 15 * KscreenHeight);
//     }
     
     
     return cell;
 }



- (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font: (CGFloat)font
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    
    return rect.size.height;
}
 
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

 return 120 * KscreenHeight;
 

 }

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//     AdetailViewController *adetailVC =  [[AdetailViewController alloc]init];
//     adetailVC.course_id = [_dataArray[indexPath.row] valueForKey:@"id"];
//     [self.navigationController pushViewController:adetailVC animated:YES];
     
     
     [Helper keChengDetailFromVC:self course_id:[_dataArray[indexPath.row] valueForKey:@"id"]];
 
 
 }

@end
